<?php
namespace app\api\controller;

use app\BaseController;

use app\common\model\VisitorModel;

use app\common\model\StaffModel;

use app\common\model\StaffDetailModel;

use app\common\model\FirmModel;

use think\db\Query;

class Visitor extends BaseController{
    
    public function list(){
        $keyword = $this->request->param('keyword/s','');
        if($keyword === ''){
            $list = VisitorModel::with(['staff','staffDetail','firm'])->where('user_id',$this->request->user_info->user_id)
            ->limit($this->request->limit)
            ->page($this->request->page)
            ->order('update_time','desc')
            ->select();
        }else{
            $list = VisitorModel::alias('v')
                ->join('staff s', 'v.staff_id = s.staff_id')
                ->join('staff_detail d', 'v.staff_id = d.staff_id')
                ->join('firm_detail f', 'v.firm_id = f.firm_id')
                ->where('user_id',$this->request->user_info->user_id)
                ->where('s.name|s.mobile|s.position|d.name_en|f.company_name','like','%'.$keyword.'%')
                ->limit($this->request->limit)
                ->page($this->request->page)
                ->order('v.update_time','desc')
                ->select();
                // ->append(['staff','firm']);
        }
        
        return success($list);
    }
    
    public function delete(){
        $visitor_id = $this->request->param('visitor_id/d',0);
        if($visitor_id === 0){
            return error('错误操作');
        }
        if(null === $VisitorModel = VisitorModel::find($visitor_id)){
            return error('内容不存在');
        }
        $VisitorModel->is_delete = 1;
        $VisitorModel->save();
        return success($atlasModel);
    }
    
    public function kefu(){
        $FirmModel = FirmModel::find($this->request->staff_info->firm_id)->append(['kefu']);
        return success($FirmModel->kefu);
    }
}